package com.okay.volume

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.okay.testclass.R

/**
 * 摄像头和麦克风的检测页面
 */
class DetectionView(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    private val TAG:String = "DetectionView"
    private var volumeUtil: VolumeUtil? = null
    private var volumeBarView: VolumeBarView? = null

    init {
        volumeUtil = context?.let { VolumeUtil(it) }

        val view: View = LayoutInflater.from(context).inflate(R.layout.view_detection, null, false)
        volumeBarView = view.findViewById(R.id.external_Settings_progress)
        addView(view)
    }

    /**
     * 开启麦克风检测
     */
    fun startRecorder() {
        volumeUtil?.startAudioRecorder(object : VolumeUtil.VolumeListener {
            override fun volumeNum(volume: Int) {
                var progress = (volume.toFloat()/40f)*100f  //这个40就是大屏采集的最大分贝值 估算的 不是准确值
                Log.d(TAG,"麦克风检测分贝值占进度的百分比：$progress")
                volumeBarView?.setSize(progress)
            }
        })
    }

    /**
     * 关闭麦克风检测
     */
    fun stopRecorder() {
        volumeUtil?.stopRecorder()
    }
}